//
//  InstallationDetailTabSignaturesView.swift
//  ezyform
//
//  Created by MoennigQ on 21/12/2016.
//  Copyright © 2016 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import SnapKit
enum OperateJobStatus: Int{
    case StartJob = 0
    case FinishJob
}
class InstallationDetailTabSignaturesView: UIView {
    let bottomView = UIView()
    let startJobBtn: UIButton = UIButton()
    let finishJobBtn: UIButton = UIButton()
    var startOrFinishJobClosure: ((OperateJobStatus)->Void)?
    let bottomViewHeight: CGFloat = 73
    var bottomViewHeightConstrain: Constraint!
    let signatureListView = SignautreListView()
    override func draw(_ rect: CGRect) {
        self.backgroundColor = APPConfig.PVDDetailBackColor
        //bottom view
        bottomView.backgroundColor = UIColor.white
        self.addSubview(bottomView)
        bottomView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            self.bottomViewHeightConstrain = make.height.equalTo(bottomViewHeight).constraint
            make.bottom.equalToSuperview()
        }
        bottomView.addSubview(startJobBtn)
        startJobBtn.snp.makeConstraints { (make) in
            make.width.equalTo(181)
            make.height.equalTo(73)
            make.top.equalToSuperview()
            make.centerX.equalToSuperview()
        }
        //
        bottomView.addSubview(finishJobBtn)
        finishJobBtn.snp.makeConstraints { (make) in
            make.edges.equalTo(startJobBtn)
        }
        //table view
        self.addSubview(signatureListView)
        signatureListView.snp.makeConstraints { (make) in
            make.left.top.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalTo(bottomView.snp.top)
            
        }
        startJobBtn.setImage(UIImage(named: "installation_detail_startjob_icon"), for: UIControl.State.normal)
        startJobBtn.addTarget(self, action: #selector(toStartJob), for: UIControl.Event.touchUpInside)
        
        finishJobBtn.setImage(UIImage(named: "installation_detail_finishjob_icon"), for: UIControl.State.normal)
        finishJobBtn.addTarget(self, action: #selector(toFinishJob), for: UIControl.Event.touchUpInside)
    }
    func updateUIByJobStatus(_ model: PVDDetailModel){
        self.signatureListView.setData(model)
        let jobStatus: String = model.job_status ?? ""
        self.bottomView.isHidden = true
        if jobStatus == "0" && model.isInstallerFlag {//scheduled且当前用户是installer
            self.bottomView.isHidden = false
            self.startJobBtn.isHidden = false
            self.finishJobBtn.isHidden = true
            self.bottomViewHeightConstrain.update(offset: bottomViewHeight)
        }else if (jobStatus == "1" || jobStatus == "3")  && model.isInstallerFlag {//installing or unqualified且当前用户是installer
            self.bottomView.isHidden = false
            self.startJobBtn.isHidden = true
            self.finishJobBtn.isHidden = false
            self.bottomViewHeightConstrain.update(offset: bottomViewHeight)
        }else {//installed,非installer
            self.bottomViewHeightConstrain.update(offset: 0)
        }
    }
    @objc func toStartJob(){
        self.startOrFinishJobClosure?(OperateJobStatus.StartJob)
    }
    @objc func toFinishJob(){
        self.startOrFinishJobClosure?(OperateJobStatus.FinishJob)
    }
}
